//
//  LGAdsView.m
//  MindBox
//
//  Created by MS on 16-1-9.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//
#define margin 10
#define ScrollWidth  self.width*0.5
#define w (self.width-2*margin)*0.5
#import "LGAdsView.h"
#import "UIImageView+WebCache.h"
#import "Gift_AdsItemView.h"
#import "UIView+Frame.h"
@interface LGAdsView ()<UIScrollViewDelegate>

@property (nonatomic,weak) UIImageView * backgroundImageView;
@property (nonatomic,weak) UIScrollView * scrollView;
@property (nonatomic,weak)  UIPageControl * pageControl;
@property (nonatomic,strong) NSTimer * timer;
@property (nonatomic,weak) Gift_AdsItemView * leftMoreView;
@property (nonatomic,weak) Gift_AdsItemView * leftView;
@property (nonatomic,weak) Gift_AdsItemView * centerView;
@property (nonatomic,weak) Gift_AdsItemView * rightView;
@property (nonatomic,weak) Gift_AdsItemView * rightMoreView;
@property (nonatomic,assign) NSInteger leftMoreIndex;
@property (nonatomic,assign) NSInteger rightMoreIndex;
@property (nonatomic,assign) NSInteger leftIndex;
@property (nonatomic,assign) NSInteger rightIndex;
@property (nonatomic,assign) NSInteger centerIndex;
@property (nonatomic,assign) NSInteger pages;
@end

@implementation LGAdsView


-(void)setGiftpageData:(GiftPageData *)giftpageData
{
    _giftpageData = giftpageData;
    
    [self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:giftpageData.backup_url]];
    
    self.imagesArray = giftpageData.banner;

}

-(void)setImagesArray:(NSArray *)imagesArray
{
    _imagesArray = imagesArray;
    
    [self.timer invalidate];
    self.timer = nil;
    _centerIndex=0;
    _pageControl.currentPage = 0;
    
    _pageControl.numberOfPages = imagesArray.count;
    
    [self setAdsItemWithView:_leftMoreView Index:_imagesArray.count-2];
    [self setAdsItemWithView:_leftView Index:_imagesArray.count-1];
    [self setAdsItemWithView:_centerView Index:0];
    [self setAdsItemWithView:_rightView Index:1];
    [self setAdsItemWithView:_rightMoreView Index:2];

    [self startTime];
}

-(void)setAdsItemWithView:(Gift_AdsItemView *)itemView Index:(NSInteger)index
{
    Gift_Banner * banner = _imagesArray[index];
    [itemView.imageView sd_setImageWithURL:[NSURL URLWithString:banner.image_url]];
    itemView.subTitleView.text = banner.good_name;
    itemView.titleView.text = banner.name;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        UIImageView * bgimageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:bgimageView];
        self.backgroundImageView = bgimageView;
        
        UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        self.scrollView = scrollView;
        [self addSubview:scrollView];
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        scrollView.delegate = self;
        scrollView.clipsToBounds =  NO;
        [self createScrollViewContent];
        scrollView.showsHorizontalScrollIndicator = NO;
        
        UIPageControl * pageControl = [[UIPageControl alloc] init];
        
        [self addSubview:pageControl];
        
        pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:1.00f];
        pageControl.pageIndicatorTintColor  =  [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:0.4];
        
        self.pageControl = pageControl;
    }
    return self ;
}


-(void)createScrollViewContent
{
    self.leftMoreView = [self createOneContentView];
    self.leftView = [self createOneContentView];
    self.centerView = [self createOneContentView];
    self.rightView = [self createOneContentView];
    self.rightMoreView =[self createOneContentView];
    
}

-(Gift_AdsItemView *)createOneContentView
{
    Gift_AdsItemView * View = [Gift_AdsItemView gift_adsViewWithXib];
    [self.scrollView addSubview:View];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidCick:)];
    [View addGestureRecognizer:tap];
    
    return View;
}

-(void)viewDidCick:(UIGestureRecognizer *)tap
{
    if ([self.delegate respondsToSelector:@selector(adsViewDidClick:index:)]) {
        [self.delegate adsViewDidClick:self index:_centerIndex];
    }    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    

    _backgroundImageView.frame = self.bounds;
    _scrollView.frame = CGRectMake(w*0.5+margin*0.5, 0.5*margin, w+margin, w);
   
    _leftMoreView.frame =CGRectMake(0.5*margin, margin, w, w);
    _leftView.frame = CGRectMake(0.5*margin+(w+margin), margin, w, w);
    _centerView.frame = CGRectMake(0.5*margin+(w+margin)*2, margin, w, w);
    _rightView.frame = CGRectMake(0.5*margin+(w+margin)*3, margin, w, w);
    _rightMoreView.frame = CGRectMake(0.5*margin+(w+margin)*4, margin, w, w);
    
    _scrollView.contentSize = CGSizeMake(5*ScrollWidth, 0);
    _scrollView.contentOffset = CGPointMake(2*ScrollWidth, 0);
    _pageControl.center = CGPointMake(self.width*0.5, self.height*0.95);
}


-(void)reloadViews
{
    //计算位置
    if (self.scrollView.contentOffset.x ==3*ScrollWidth) {
        _centerIndex  = (_centerIndex == _imagesArray.count-1) ? 0 : _centerIndex+1;
    }
    else if (self.scrollView.contentOffset.x ==ScrollWidth)
    {
        _centerIndex = (_centerIndex == 0)? _imagesArray.count-1 : _centerIndex-1;
    }
    
    _leftIndex = (_centerIndex == 0) ? (_imagesArray.count-1) : (_centerIndex-1);
    _rightIndex = (_centerIndex == _imagesArray.count-1) ? 0 : (_centerIndex+1);
    
    _leftMoreIndex =(_leftIndex == 0) ? (_imagesArray.count-1) : (_leftIndex-1);
    _rightMoreIndex = (_rightIndex == _imagesArray.count-1) ? 0 : (_rightIndex+1);
    
    //设置中间View
    [self setAdsItemWithView:_leftMoreView Index:_leftMoreIndex];
    [self setAdsItemWithView:_leftView Index:_leftIndex];
    [self setAdsItemWithView:_centerView Index:_centerIndex];
    [self setAdsItemWithView:_rightView Index:_rightIndex];
    [self setAdsItemWithView:_rightMoreView Index:_rightMoreIndex];
    
    self.centerView.tag = _centerIndex;
    
    self.pageControl.currentPage = _centerIndex;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self reloadViews];
    
    self.scrollView.contentOffset = CGPointMake(2*ScrollWidth, 0);

}

//自动滚动setContentOffset完成后调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self reloadViews];
 
    [self.scrollView setContentOffset:CGPointMake(2*ScrollWidth, 0) animated:NO];
}


-(void) startTime
{
    NSTimer * timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    self.timer = timer ;
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void) autoScroll
{
    
    [self.scrollView setContentOffset:CGPointMake( 3*ScrollWidth, 0) animated:YES];
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    self.timer = nil;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
        [self startTime];
}


@end
